//
//  CapturePreviewView.m
//  Unity-iPhone
//
//  Created by Karl on 2017/3/29.
//
//

#import "CapturePreviewView.h"


@implementation CapturePreviewView{
    UIView *_bg;
    UIView *_previewBg;
    UIButton *_fbshareBtn;
    UIButton *_donateBtn;
    UIButton *_closeBtn;
    UIImageView *_imageView;
    UIImageView *_savedView;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        _bg = [UIView new];
        [_bg setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.6]];
        [self addSubview:_bg];
        
        _previewBg = [UIView new];
        [_previewBg setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:_previewBg];
        
        _imageView = [UIImageView new];
//        [_imageView setImage:[UIImage imageNamed:@"preview_bg_cn"]];
        [_previewBg addSubview:_imageView];
        
        _fbshareBtn = [UIButton new];
        [_fbshareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
        [_fbshareBtn setBackgroundImage:kImgFromFile(Babel(@"capture_preview_share"), @"png") forState:UIControlStateNormal];
        [_previewBg addSubview:_fbshareBtn];
        
        _donateBtn = [UIButton new];
        [_donateBtn setBackgroundImage:kImgFromFile(Babel(@"capture_preview_donate"), @"png")forState:UIControlStateNormal];
        [_donateBtn addTarget:self action:@selector(donateAction:) forControlEvents:UIControlEventTouchUpInside];
        [_previewBg addSubview:_donateBtn];
        
        _closeBtn = [UIButton new];
        [_closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_closeBtn setBackgroundImage:[UIImage imageNamed:@"all_close"] forState:UIControlStateNormal];
        [_previewBg addSubview:_closeBtn];
        
        _savedView = [UIImageView new];
        [_savedView setImage:kImgFromFile(Babel(@"capture_preview_saved"), @"png")];
        [_previewBg addSubview:_savedView];
        

    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat scale = 1.0;
    if (isPad) {
        scale = SizeScaleX;
    }else{
        scale = SizeScaleY;
    }

    
    _bg.left = 0;
    _bg.top = 0;
    _bg.width = self.width;
    _bg.height = self.height;
    
    _previewBg.centerY = self.height/2;
    _previewBg.width = 532.7 * scale;
    _previewBg.height = 390 * scale;
    _previewBg.left = 100 * scale;
    
    _imageView.left = 0 * scale;
    _imageView.top = 0 * scale;
    _imageView.width = _previewBg.width;
    _imageView.height = 324.3 * scale;
    
    _savedView.centerX = _previewBg.width/2;
    _savedView.top = 0;
    _savedView.width = 104.7 * scale;
    _savedView.height = 26*scale;
    
    _closeBtn.width = 30;
    _closeBtn.height = 30;
    _closeBtn.top = 1.4;
    _closeBtn.right = _previewBg.width-1.4;
    
    _fbshareBtn.width = 168.7 * scale;
    _fbshareBtn.height = 57 * scale;
    _fbshareBtn.bottom = _previewBg.height - 4.7 * scale;
    _fbshareBtn.right = _previewBg.width/2 - 36 * scale;
    
    _donateBtn.width = 168.7 * scale;
    _donateBtn.height = 57 * scale;
    _donateBtn.bottom = _previewBg.height - 4.7 * scale;
    _donateBtn.left = _previewBg.width/2 + 36 * scale;
}

- (void)closeAction:(id)sender{
    self.hidden = YES;
}

- (void)shareAction:(id)sender{
    [[NativeViewManager sharedManager] shareImage:[_imageView.image copy]];
    }
- (void)donateAction:(id)sender{
    [[NativeViewManager sharedManager] showWebWithURL:Babel(@"donate_url")];
}
- (void)show{
    _savedView.hidden = YES;
    _imageView.image = nil;
    [_imageView setImage:[[NativeViewManager sharedManager] captureWithFrame]];
    self.hidden = NO;
    
    [self saveImageToAlbum:_imageView.image];
    
}
- (void)saveImageToAlbum:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    _savedView.hidden = NO;
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}
@end
